package com.admin.common.util;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

import org.apache.commons.collections.CollectionUtils;
import org.springframework.util.StringUtils;

/**
 * 字符串工具类
 * 
 * @author ZY
 * @date 2018/12/04
 */
public class YsStringUtil {

    /**
     * 拆分字符串为List<Integer>
     * 
     * @param str
     * @param regex
     * @return
     */
    public static List<Integer> splitToInteger(String str, String regex) {
        List<Integer> list = new ArrayList<Integer>();
        if (!StringUtils.isEmpty(str)) {
            for (String s : str.split(regex)) {
                list.add(Integer.valueOf(s));
            }
        }
        return list;
    }

    /**
     * 拆分字符串为List<Long>
     * 
     * @param str
     * @param regex
     * @return
     */
    public static List<Long> splitToLong(String str, String regex) {
        List<Long> list = new ArrayList<Long>();
        if (!StringUtils.isEmpty(str)) {
            for (String s : str.split(regex)) {
                list.add(Long.valueOf(s));
            }
        }
        return list;
    }

    /**
     * 拆分字符串为List<String>
     * 
     * @param str
     * @param regex
     * @return
     */
    public static List<String> splitToString(String str, String regex) {
        List<String> list = new ArrayList<String>();
        if (!StringUtils.isEmpty(str)) {
            for (String s : str.split(regex)) {
                list.add(s);
            }
        }
        return list;
    }

    /**
     * 
     * @param list
     * @param regex
     * @return
     */
    public static String joinIntegerToString(List<Integer> list, String regex) {
        StringBuffer sb = new StringBuffer("");
        if (CollectionUtils.isNotEmpty(list) && !StringUtils.isEmpty(regex)) {
            for (Integer i : list) {
                sb.append(i).append(regex);
            }
            return sb.substring(0, sb.indexOf(regex));
        }
        return sb.toString();
    }

    public static String joinStringToString(List<String> list, String regex) {
        if (CollectionUtils.isEmpty(list) || org.apache.commons.lang3.StringUtils.isBlank(regex)) {
            return null;
        }
        StringBuffer sb = new StringBuffer();
        for (String i : list) {
            sb.append(i).append(regex);
        }
        return sb.deleteCharAt(sb.lastIndexOf(regex)).toString();
    }

    /**
     * 根据支付标记返货支付中文方式
     * 
     * @param payType
     * @return
     */
    public static String getPayTypeExplain(String payType) {
        if (Objects.isNull(payType)) {
            return "";
        }
        switch (payType) {
            case "cash":
                return "线下支付";
            case "alipay":
                return "支付宝";
            case "tenpay":
                return "微信";
            case "card":
                return "储蓄卡";
            case "creditCard":
                return "信用卡";
            case "ebank":
                return "个人网银";
            case "b2b_ebank":
                return "企业网银";
            case "balance":
                return "个人钱包";
            case "b2b_balance":
                return "企业钱包";
            default:
                return "";

        }
    }

    /**
     * 过滤emoji 或者 其他非文字类型的字符
     *
     * @param source
     * @return
     */
    public static String filterEmoji(String source) {
        if (isBlank(source)) {
            return source;
        }
        StringBuilder buf = null;
        int len = source.length();
        for (int i = 0; i < len; i++) {
            char codePoint = source.charAt(i);
            if (isEmojiCharacter(codePoint)) {
                if (buf == null) {
                    buf = new StringBuilder(source.length());
                }
                buf.append(codePoint);
            }
        }
        if (buf == null) {
            return source;
        } else {
            if (buf.length() == len) {
                buf = null;
                return source;
            } else {
                return buf.toString();
            }
        }
    }

    /**
     * 空字符检测
     *
     * @param cs
     * @return
     */
    public static boolean isBlank(final CharSequence cs) {
        int strLen;
        if (cs == null || (strLen = cs.length()) == 0) {
            return true;
        }
        for (int i = 0; i < strLen; i++) {
            if (Character.isWhitespace(cs.charAt(i)) == false) {
                return false;
            }
        }
        return true;
    }

    private static boolean isEmojiCharacter(char codePoint) {
        return (codePoint == 0x0) || (codePoint == 0x9) || (codePoint == 0xA) || (codePoint == 0xD)
            || ((codePoint >= 0x20) && (codePoint <= 0xD7FF)) || ((codePoint >= 0xE000) && (codePoint <= 0xFFFD));
    }
    
    /**
     * 获取字符串中${}中的key值并且
     *
     * @param text
     * @return
     */
    public static List<String> getRuleKey(String text) {
        String openToken = "${";
        String closeToken = "}";

        List<String> list = new ArrayList<>();
        if (org.apache.commons.lang3.StringUtils.isBlank(text)) {
            return list;
        }
        if (text.indexOf(openToken) < 0) {
            return list;
        }
        Boolean sign = false;
        int openIndex = 0;
        int closeIndex = 0;
        int searchIndex = 0;
        do {
            sign = false;
            openIndex = text.indexOf(openToken, searchIndex);
            closeIndex = text.indexOf(closeToken, searchIndex);
            if (openIndex > -1 && closeIndex > -1) {
                String substring = text.substring(openIndex + 2, closeIndex);
                searchIndex = closeIndex + 1;
                list.add(substring);
                sign = true;
            }
        } while (sign);
        return list;
    }
}
